package com.imooc;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FFMpegTest {

    private String ffmpegExePath;

    public FFMpegTest(String ffmpeg) {
        this.ffmpegExePath = ffmpeg;
    }

    public static void main(String[] args) {


        FFMpegTest ffMpegTest = new FFMpegTest("D:\\develop\\ffmpeg\\bin\\ffmpeg.exe");
        for (int i = 0; i < 2; i++) {
            Random random = new Random();
            int index = random.nextInt();
            ffMpegTest.convertor("D:\\develop\\ffmpeg\\bin\\1.mp4", "D:\\develop\\ffmpeg\\bin\\" + index + ".mp4");

        }
    }


    public void convertor(String inputFilePath, String outputFilePath) {


        List<String> command = new ArrayList<String>();
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(inputFilePath);
      /*  command.add("-f");
        command.add("image2");
        command.add("-ss");
        command.add("10");
        command.add("-t");
        command.add("0.001");
        command.add("-s");
        command.add("320*240");*/
        command.add(outputFilePath);

        ProcessBuilder builder = new ProcessBuilder();

        builder.command(command);

        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        try {
            //开始执行命令
            Process process = builder.start();
            //如果你想获取到执行完后的信息，那么下面的代码也是需要的
            StringBuffer sbf = new StringBuffer();
            String line = null;
            InputStream inputStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(inputStreamReader);
            while ((line = br.readLine()) != null) {
                sbf.append(line);
                sbf.append(" ");
            }
            String resultInfo = sbf.toString();
            System.out.println(resultInfo);
            if (br != null) {
                br.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
